package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.os.Bundle
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_add_car.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 添加至出货车辆
 */
class AddCarActivity : BaseActivity<HomeViewModel>() {
    override fun layoutId(): Int = R.layout.activity_add_car

    override fun initView(savedInstanceState: Bundle?) {
    }

    override fun initData() {
        ie_carType.mEdit.isSingleLine = false
        viewModel.getDeliverTruck()
    }

    override fun initListener() {


        ie_carType.mEdit.setOnClickListener {
            ie_carType.showListDialog("请选择车辆", this)
        }

        bt_commit.setOnClickListener {
            if (ie_channelOrderNo.mEdit.text.isNullOrEmpty()) {
                ToastUtils.showShort("请输入渠道单号")
                return@setOnClickListener
            }

            val params: Param = Param::class.java.newInstance()
            params.dispatchCarId =
                viewModel.mGetDeliverTruck.value!!.data[ie_carType.mCurrentPosition].dispatchCarId
            params.channelOrderNo = ie_channelOrderNo.mEdit.text.toString()
            viewModel.addDeliverCar(params)
        }


        viewModel.mGetDeliverTruck.observe(this, {
            if (it.isSuccess()) {
                it.data.forEach {
                    ie_carType.mData.add(
                        it.license + "--->" +
                                it.destination.substring(0, 2) +
                                getTime(it.planTime)
                    )
                }
                ie_carType.mEdit.setText(ie_carType.mData[0])
            }
        })

        viewModel.mAddDeliverCar.observe(this, {
            if (it.isSuccess()) {
                speakString("添加成功")
                finish()
            }
        })

    }

    @SuppressLint("SimpleDateFormat")
    fun getTime(time: Long): String? {
        if (time == 0L) {
            return ""
        }
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val d1 = Date(time)
        return "\n" + format.format(d1)
    }

}